/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.Customer;
import com.startup.musicstore.model.OrderItem;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.services.AlbumSalesService;
import com.startup.musicstore.services.crud.AlbumCrudService;
import com.startup.musicstore.services.crud.CustomerCrudService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author linda
 */
@Service("albumSalesService")
@Transactional
public class AlbumSalesServiceImpl implements AlbumSalesService{
    
    @Autowired
    private CustomerCrudService customerCrudService;
    
    @Autowired
    private AlbumCrudService albumCrudService;

    @Override
    public void AlterAlbumSales() {
        
        int quantity;
        int sales;
        int inventory;
        
        List<Customer> customers = customerCrudService.findAll();
        for(Customer customer : customers)
        {
            List<Orders> orders = customer.getOrders();
            for(Orders order: orders)
            {
                List<OrderItem> items = order.getItems();
                for(OrderItem item : items)
                {
                    Album album = item.getAlbum();
                    
                    quantity = item.getQuantity();
                    sales = album.getSales() + quantity;
                    inventory = album.getInventory() - quantity;
                    
                    album.setInventory(inventory);
                    album.setSales(sales);
                    
                    albumCrudService.merge(album);
                }
            }
            
        }
        
    }
    
}
